package com.applet.shoppingcart.controller;


import com.applet.context.AlibabaTTL;
import com.applet.dto.ShoppingCartDTO;
import com.applet.entity.ShoppingCart;
import com.applet.result.Result;
import com.applet.shoppingcart.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 购物车 controller
 */
@Slf4j
@RestController
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {


    @Resource
    private ShoppingCartService shoppingCartService;



    /**
     * 购物车管理
     *          添加购物车数据 (所属某一个用户的购物车，从ThreadLocal中获取用户id)
     */
    @PostMapping("/add")
    public Result getUserShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("购物车管理 - 当前用户id: {},添加购物车数据: {}", AlibabaTTL.threadLocalGet().toString(), shoppingCartDTO);
        shoppingCartService.addUserShoppingCartList(shoppingCartDTO);
        return Result.success();
    }



    /**
     * 购物车管理
     *         查询当前用户的购物车数据  可选参数
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> getUserShopCart(Long userId){
        log.info("购物车管理 - 查询当前用户id: {}的购物车数据", userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.selectUserShopCart(userId);
        return Result.success(shoppingCartList);
    }



    /**
     * 购物车管理
     *          清空当前用户的购物车     可选参数
     */
    @DeleteMapping("/clean")
    public Result deleteUserShopCart(Long userId){
        log.info("购物车管理 - 清空当前用户id: {}的购物车", userId);
        shoppingCartService.deleteUserShopCart(userId);
        return Result.success();
    }



    /**
     * 购物车管理
     *         减少 菜品/套餐 的数量
     */
    @PostMapping("/sub")
    public Result deleteUserShopCartCopies(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("购物车管理 - 减少购物车菜品/套餐数量: {}", shoppingCartDTO);
        shoppingCartService.deleteUserShopCartCopies(shoppingCartDTO);
        return Result.success();
    }





}
